<template>
  <div class="goods-list">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>商品列表</span>
      </div>
      <el-table :data="tableData">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品 ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="商品名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="商品分类">
                <span>{{ props.row.category }}</span>
              </el-form-item>
              <el-form-item label="商品价格">
                <span>{{ props.row.price }}</span>
              </el-form-item>
              <el-form-item label="商品图片">
                <el-image
                  style="width: 100px; height: 100px"
                  :src="props.row.imgUrl"
                  fit="fill"
                ></el-image>
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ props.row.ctime | formatYMDhms }}</span>
              </el-form-item>
              <el-form-item label="商品评价">
                <span>{{ props.row.rating }}</span>
              </el-form-item>
              <el-form-item label="商品销量">
                <span>{{ props.row.sellCount }}</span>
              </el-form-item>
              <el-form-item label="商品描述">
                <span>{{ props.row.goodsDesc }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="name"> </el-table-column>
        <el-table-column label="所属分类" prop="category"> </el-table-column>
        <el-table-column label="商品价格" prop="price"> </el-table-column>
        <el-table-column label="商品图片" prop="imgUrl">
          <template slot-scope="{ row }">
            <el-image
              style="width: 100px; height: 100px"
              :src="row.imgUrl"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="商品描述" prop="goodsDesc"> </el-table-column>
        <el-table-column label="操作" prop="name" width="150">
          <template slot-scope="{ row }">
            <el-button size="mini" @click="dialogTrue(row)">编辑</el-button>
            <el-popconfirm
              title="这是一段内容确定删除吗？"
              @confirm="handelDel(row.id)"
            >
              <el-button
                size="mini"
                type="danger"
                style="margin-left: 7px"
                slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[3, 5]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 抽屉 -->
    <el-drawer title="我是标题" :visible.sync="drawer" direction="rtl">
      <el-form ref="formRef" :model="goodsForm" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="goodsForm.name"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select v-model="goodsForm.category" placeholder="请选择商品分类">
            <el-option
              v-for="item in category"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input-number v-model="goodsForm.price" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-image
            style="width: 100px; height: 100px"
            :src="goodsForm.imgUrl"
            fit="fill"
          ></el-image>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input type="textarea" v-model="goodsForm.goodsDesc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleConfirm">确定</el-button>
          <el-button class="delBtn">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { goodsCategories, goodsEdit, goodsList, goodsDel } from "@/api/goods";
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      drawer: false,
      loading: false,
      formData: {
        account: "",
        password: "",
        userGroup: "",
      },
      goodsForm: {
        name: "",
        category: "",
        goodsDesc: "",
        price: "",
        imgUrl: "",
      },
      formLabelWidth: "80px",
      timer: null,
      category: [],
    };
  },
  created() {
    this.getList();
    this.getCategoryName();
  },
  methods: {
    //获取列表
    async getList() {
      let res = await goodsList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
      });
      console.log(res);
      let { data, total } = res.data;
      this.total = total;
      this.tableData = data;
    },
    //当前页面
    handleCurrentChange(val) {
      console.log(val);
      this.currentPage = val;
      this.getList();
    },
    //每页条数
    handleSizeChange(val) {
      console.log(val);
      this.pageSize = val;
      this.getList();
    },
    //删除
    async handelDel(id) {
      // console.log(row);
      let res = await goodsDel({ id });
      console.log(res);
      this.getList();
    },
    //查看启用分类名称
    async getCategoryName() {
      let res = await goodsCategories();
      this.category = res.data;
      console.log(this.category);
    },
    //编辑
    dialogTrue(row) {
      // console.log(row);
      this.drawer = true;
      //数据回填
      this.goodsForm = { ...row };
    },
    //点击确认修改
    async handleConfirm() {
      let res = await goodsEdit(this.goodsForm);
      this.drawer = false;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.goods-list {
  padding: 0 20px;
  margin-top: 15px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.el-button--danger {
  background-color: #d99bac;
  border: 0;
}
.block {
  margin-top: 20px;
}
.el-button--primary {
  background-color: #d99bac;
  border: 0;
  margin-left: 10px;
}
.delBtn {
  margin-left: 45px;
}
</style>